<template>
  <div class="user">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input v-model="searchParams.name" placeholder="请输入擂台名称" clearable style="width:200px"
          size="mini"></el-input>
        <el-select v-model="searchParams.catId" size="mini" placeholder="请选择擂台类型" clearable>
          <el-option v-for="item in catData" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-select v-model="searchParams.status" size="mini" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusDatas" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-date-picker v-model="searchParams.startTime" type="datetime" placeholder="选择开始时间"
          :picker-options="pickerOptions" size="mini"></el-date-picker>
        <el-date-picker v-model="searchParams.endTime" type="datetime" placeholder="选择结束时间"
          :picker-options="pickerOptions" size="mini"></el-date-picker>
        <el-button type="primary" size="mini" @click="getData" v-if="has('challengeBox/index')">搜索
        </el-button>
        <el-button type="info" size="mini" @click="reset" v-if="has('challengeBox/index')">重置
        </el-button>
        <el-button type="primary" @click="add()" size="mini" v-if="has('challengeBox/store')">添加
        </el-button>
      </el-row>
      <el-table :data="data.data" v-loading="table_loading" style="width: 100%;margin:10px auto" size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }">
        <el-table-column fixed prop="id" label="擂台信息" align="center" width="160px">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <el-image :src="scope.row.image" :style="{
                width: '50px',
                height: '50px',
                borderRadius: '5px',
                marginRight: '10px'
              }" :preview-src-list="[scope.row.image]" fit="contain"></el-image>
              <div>
                <div>{{ scope.row.name }}</div>
                <div>ID:{{ scope.row.id }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="cat_id_name" label="类型" align="center">
          <template slot-scope="scope">
            <span :style="{
              backgroundColor: scope.row.cat_id_color,
              color: '#fff',
              padding: '10px',
              height: '20px',
              lineHeight: '20px',
              textAlign: 'center',
              width: '100px',
              borderRadius: '10px'
            }">
              {{ scope.row.cat_id_name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="单抽价" align="center"></el-table-column>
        <!--        <el-table-column-->
        <!--          prop="boxs"-->
        <!--          label="箱数"-->
        <!--          align="center"-->
        <!--        ></el-table-column>-->
        <el-table-column prop="sales" label="销量" align="center"></el-table-column>
        <el-table-column prop="total_income" label="总收入" align="center"></el-table-column>
        <el-table-column prop="total_pay" label="总支出" align="center"></el-table-column>
        <el-table-column prop="diff" label="差值" align="center">
          <template slot-scope="scope">
            {{ (scope.row.total_income - scope.row.total_pay).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="custom_user_id" label="定制人ID" align="center"></el-table-column>
        <el-table-column prop="custom_end_at" label="定制结束日期" align="center"></el-table-column>
        <el-table-column prop="created_at" label="时间" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-switch @change="changeStatus(scope.row)" v-model="scope.row.status" active-color="#13ce66"
              inactive-color="#ff4949" :active-value="activeValue" :inactive-value="inactiveValue">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="300">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="detail(scope.row.id)" v-if="has('challengeBox/show')">编辑
            </el-button>
            <el-button type="primary" size="mini" @click="prize(scope.row.id)" v-if="has('challengeBoxPrizes/index')">赏品
            </el-button>
            <el-button type="primary" size="mini" @click="log(scope.row)"
              v-if="has('challengeBoxOrderPrizes/record')">记录
            </el-button>
            <el-button type="primary" size="mini" @click="showRenwalDialog(scope.row.id)"
              v-if="has('challengeBoxPrizes/renewBoxs')">续箱
            </el-button>
            <el-button type="danger" size="mini" @click="del(scope.row.id, scope.$index)"
              v-if="has('challengeBox/destroy')">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background style="text-align:center;margin:20px 0;" :current-page="searchParams.page"
        :page-sizes="pageSizes" layout="total,prev, pager, next,sizes" :total="data.total" @size-change="changeSize"
        @current-change="changePage"></el-pagination>
    </el-card>

    <el-card v-if="status === 'add'">
      <div slot="header">
        <el-page-header @back="back()" content="添加擂台"></el-page-header>
      </div>
      <el-form ref="form" :rules="rules" :model="editParams" label-width="120px">
        <el-form-item label="擂台名称" prop="name">
          <el-input v-model="editParams.name" style="width: 300px;"></el-input>
        </el-form-item>

        <el-form-item label="擂台类型" prop="cat_id">
          <el-select v-model="editParams.cat_id" placeholder="请选择擂台类型" clearable>
            <el-option v-for="item in catData" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="单抽价格" prop="price">
          <el-input v-model="editParams.price" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="定制人ID" prop="custom_user_id">
          <el-input v-model="editParams.custom_user_id" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="定制天数" prop="custom_days">
          <el-input v-model="editParams.custom_days" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="临界值" prop="income_value">
          <el-input v-model="editParams.income_value" style="width: 300px;"></el-input>
        </el-form-item>

        <el-form-item label="临界商品id" prop="income_id">
          <el-input v-model="editParams.income_id" style="width: 300px;"></el-input>
        </el-form-item>

        <el-form-item label="图片" prop="image">
          <el-button size="mini" type="primary" @click="openImage(2, false, {})">选择商品库
          </el-button>
          <div>
            <el-image v-if="editParams.image" :src="editParams.image" :preview-src-list="[editParams.image]"
              style="height: 200px" fit="contain">
            </el-image>
          </div>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input v-model="editParams.sort" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form', 1)" v-if="has('challengeBox/store')">保 存
          </el-button>
          <el-button type="info" @click="back()" v-if="has('challengeBox/store')">取 消
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card v-if="status === 'edit'">
      <div slot="header">
        <el-page-header @back="back()" content="编辑盲盒"></el-page-header>
      </div>
      <el-form ref="form" :rules="rules" :model="editParams" label-width="120px">
        <el-form-item label="盲盒名称" prop="name">
          <el-input v-model="editParams.name" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="盲盒类型" prop="cat_id">
          <el-select v-model="editParams.cat_id" placeholder="请选择盲盒类型" clearable>
            <el-option v-for="item in catData" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单抽价格" prop="price">
          <el-input v-model="editParams.price" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="定制人ID" prop="custom_user_id">
          <el-input v-model="editParams.custom_user_id" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="定制天数" prop="custom_days">
          <el-input v-model="editParams.custom_days" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="临界值" prop="income_value">
          <el-input v-model="editParams.income_value" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="临界商品id" prop="income_id">
          <el-input v-model="editParams.income_id" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <el-button size="mini" type="primary" @click="openImage(2, false, {})">选择商品库
          </el-button>
          <div>
            <el-image v-if="editParams.image" :src="editParams.image" :preview-src-list="[editParams.image]"
              style="height: 200px" fit="contain">
            </el-image>
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="editParams.sort" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form', 2)" v-if="has('challengeBox/store')">保 存
          </el-button>
          <el-button type="info" @click="back()" v-if="has('challengeBox/store')">取 消
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card v-if="status === 'prize'">
      <div slot="header">
        <el-page-header @back="back()" content="赏品列表"></el-page-header>
      </div>
      <el-row>
        <el-input v-model="prizeSearchParams.name" placeholder="请输入赏品名称" clearable style="width:200px"
          size="mini"></el-input>
        <el-input v-model="prizeSearchParams.boxs" placeholder="请输入箱数" clearable style="width:200px"
          size="mini"></el-input>
        <el-select v-model="prizeSearchParams.levelId" style="width:200px" clearable size="mini" placeholder="请选择赏品等级">
          <el-option v-for="item in levelData" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>

        <el-button type="primary" size="mini" @click="getPrize" v-if="has('challengeBoxPrizes/index')">搜索
        </el-button>
        <el-button type="primary" @click="addPrize()" size="mini" v-if="has('challengeBoxPrizes/store')">添加
        </el-button>
      </el-row>
      <el-table :data="prizeData.data" v-loading="prizeTableLoading" style="width: 100%;margin:10px auto" size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }">
        <el-table-column fixed prop="id" label="赏品信息" align="center" width="250px" :key="Math.random()">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <img :src="scope.row.image" :style="{
                width: '50px',
                height: '50px',
                borderRadius: '5px',
                marginRight: '10px'
              }" @click="bigImage(scope.row.image)" />
              <div>
                <div>赏品ID：{{ scope.row.id }}</div>
                <div>{{ scope.row.name }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="level_name" label="等级" align="center" :key="Math.random()">
          <template slot-scope="scope">
            <span :style="{
              backgroundColor: scope.row.level_color,
              color: '#fff',
              padding: '10px',
              height: '20px',
              lineHeight: '20px',
              textAlign: 'center',
              width: '100px',
              borderRadius: '10px'
            }">
              {{ scope.row.level_name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column :key="Math.random()" prop="floors" label="层级" align="center"></el-table-column>
        <el-table-column :key="Math.random()" prop="empty_num" label="空几" align="center"></el-table-column>
        <el-table-column :key="Math.random()" prop="bunchs" label="串几" align="center"></el-table-column>
        <el-table-column :key="Math.random()" prop="boxs" label="箱数" align="center"></el-table-column>

        <el-table-column :key="Math.random()" prop="price" label="价格" align="center"></el-table-column>
        <el-table-column :key="Math.random()" prop="recovery_price" label="兑换价" align="center" v-slot:default="{ row }">
          <el-input @blur="updateRecoveryPrice(row)" @click.native="row.focus = true"
            v-model="row.recovery_price"></el-input>
        </el-table-column>
        <el-table-column :key="Math.random()" prop="show_price" label="参考价" align="center" v-slot:default="{ row }">
          <el-input @blur="updatePrice(row)" @click.native="row.focus = true" v-model="row.show_price"></el-input>
        </el-table-column>
        <el-table-column prop="num" label="数量" align="center" :key="Math.random()"></el-table-column>

        <el-table-column prop="probability" label="概率" align="center" :key="Math.random()" v-slot:default="{ row }">
          <el-input @blur="updateProbability(row)" @click.native="row.focus = true"
            v-model="row.probability"></el-input>
        </el-table-column>
        <el-table-column prop="created_at" label="时间" align="center" :key="Math.random()"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="200" :key="Math.random()">
          <template slot-scope="scope">
            <!-- <el-button
              type="primary"
              size="mini"
              @click="editPrize(scope.row.id)"
              v-if="has('blindBox/show')"
              >编辑</el-button
            > -->
            <el-button type="danger" size="mini" @click="delPrize(scope.row.id, scope.$index)"
              v-if="has('challengeBoxPrizes/destroy')">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background style="text-align:center;margin:20px 0;" :current-page="prizeSearchParams.page"
        :page-sizes="pageSizes" layout="total,prev, pager, next,sizes" :total="prizeData.total"
        @size-change="changePrizeSize" @current-change="changePrizePage"></el-pagination>
    </el-card>

    <el-dialog title="已选择用户" :visible.sync="giveShow" :close-on-click-modal="false">
      <el-button @click="addGiveUser" type="primary">添加用户</el-button>
      <el-table :data="selectUsers" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column label="ID" width="120" align="center">
          <template v-slot:default="{ row }">{{ row.id }}</template>
        </el-table-column>
        <el-table-column prop="avatar" label="用户头像" align="center">
          <template v-slot:default="{ row }">
            <div style="display: flex;align-items: center; justify-content: center;">
              <el-image :src="row.avatar" :style="{
                width: '50px',
                height: '50px',
                borderRadius: '5px',
                marginRight: '10px'
              }" :preview-src-list="[row.avatar]" fit="contain"></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="昵称">
          <template v-slot:default="{ row }">{{ row.nickname }}</template>
        </el-table-column>
        <el-table-column label="手机号码">
          <template v-slot:default="{ row }">{{ row.mobile }}</template>
        </el-table-column>
      </el-table>
      <!--      <div slot="footer" class="dialog-footer">-->
      <!--        <el-button type="primary" @click="giveSubmit">赠送</el-button>-->
      <!--      </div>-->
    </el-dialog>
    <el-card v-show="status === 'log'">
      <div slot="header">
        <el-page-header @back="back()"
          :content="'抽奖记录:' + logTitle + ':' + logSearchParams.blind_box_id"></el-page-header>
      </div>
      <el-row>
        <el-input v-model="logSearchParams.userId" placeholder="请输入用户id" clearable style="width:200px"
          size="mini"></el-input>
        <el-select v-model="logSearchParams.levelId" style="width:200px" clearable size="mini" placeholder="请选择赏品等级">
          <el-option v-for="item in levelData" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-date-picker v-model="logSearchParams.startTime" type="datetime" placeholder="选择开始时间"
          size="mini"></el-date-picker>
        <el-date-picker v-model="logSearchParams.endTime" type="datetime" placeholder="选择结束时间"
          size="mini"></el-date-picker>
        <el-button type="primary" size="mini" @click="getLog" v-if="has('blindBoxOrderPrizeLogs/index')">搜索
        </el-button>
      </el-row>
      <el-table :data="logData.data" v-loading="logTableLoading" style="width: 100%;margin:10px auto" size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }">
        <el-table-column label="用户信息" align="center" prop="user_id" width="150px">
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-image :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]" fit="contain" :style="{
                width: '50px',
                height: '50px',
                borderRadius: '5px',
                marginRight: '10px',
                alignItems: 'center'
              }"></el-image>
              <div style="display: flex;flex-direction: column;justify-content: space-between;">
                <span>{{ scope.row.nickname }}</span>
                <span>ID:{{ scope.row.user_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="赏品信息" align="center" width="160px">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <el-image :src="scope.row.box_prize_image" :preview-src-list="[scope.row.box_prize_image]" :style="{
                width: '50px',
                height: '50px',
                marginRight: '10px',
                alignItems: 'center'
              }"></el-image>
              <div>
                <div>{{ scope.row.box_prize_name }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="level_name" label="等级" align="center">
          <template slot-scope="scope">
            <span :style="{
              backgroundColor: scope.row.level_color,
              color: '#fff',
              padding: '10px',
              height: '20px',
              lineHeight: '20px',
              textAlign: 'center',
              width: '100px',
              borderRadius: '10px'
            }">
              {{ scope.row.level_name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="num" label="数量" align="center"></el-table-column>
        <el-table-column prop="price" label="价值" align="center"></el-table-column>
        <el-table-column prop="recovery_price" label="兑换价" align="center"></el-table-column>
        <el-table-column prop="created_at" label="创建时间" align="center"></el-table-column>
      </el-table>
      <el-pagination background style="text-align:center;margin:20px 0;" :current-page="logSearchParams.page"
        :page-sizes="pageSizes" layout="total,prev, pager, next,sizes" :total="logData.total"
        @size-change="changeLogSize" @current-change="changeLogPage"></el-pagination>
    </el-card>

    <el-dialog title="确定赏品" :visible.sync="prizeStatus" width="80%" :close-on-click-modal="false">
      <el-row>
        <el-button type="primary" @click="selectPrize()" size="mini" v-if="has('challengeBoxPrizes/store')">选择赏品
        </el-button>
      </el-row>
      <el-table :data="submitPrizeData" v-if="submitPrizeData.length > 0">
        <el-table-column fixed prop="id" label="赏品信息" align="center" width="160px">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <img :src="scope.row.image" :style="{
                width: '50px',
                height: '50px',
                borderRadius: '5px',
                marginRight: '10px'
              }" @click="bigImage(scope.row.image)" />
              <div>
                <div>{{ scope.row.name }}</div>
                <div>{{ scope.row.id }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="level_id" label="等级" align="center">
          <template slot-scope="scope">
            <span :style="{
              backgroundColor: scope.row.level_color,
              color: '#fff',
              padding: '10px',
              height: '20px',
              lineHeight: '20px',
              textAlign: 'center',
              width: '100px',
              borderRadius: '10px'
            }">
              {{ scope.row.lt_level.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="层级" prop="floors">
          <template slot-scope="scope">
            <el-select v-model="scope.row.floors" placeholder="请选择层级" clearable>
              <el-option v-for="item in floorsData" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" align="center"></el-table-column>
        <el-table-column prop="recovery_price" label="兑换价" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.recovery_price" placeholder="请输入兑换价"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="num" label="数量" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.num" placeholder="请输入数量"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="empty_num" label="空几必中" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.empty_num" placeholder="请输入空几必中"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="bunchs" label="串" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.bunchs" placeholder="请输入串"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="min_fa" label="随机赠送最小" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.min_fa" placeholder="请输入随机赠送最小"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="max_fa" label="随机赠送最大" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.max_fa" placeholder="请输入随机赠送最大"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="probability" label="概率" align="center" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.probability" placeholder="请输入概率">
              <template slot="append">%</template>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="时间" align="center"></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addPrizeSubmit">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择赏品" :visible.sync="addPrizeStatus" :close-on-click-modal="false">
      <el-row>
        <el-select v-model="selectPrizeParams.levelId" style="width:200px" clearable size="mini" placeholder="请选择赏品等级">
          <el-option v-for="item in levelData" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-button type="primary" @click="getSelectPrize()" size="mini">搜索
        </el-button>
      </el-row>
      <el-table :data="selectPrizeData.data" @selection-change="changePrize" v-loading="addPrizeLoading">
        <el-table-column type="selection" width="55"></el-table-column>

        <el-table-column fixed prop="id" label="赏品信息" align="center" width="160px">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;">
              <img :src="scope.row.image" :style="{
                width: '50px',
                height: '50px',
                borderRadius: '5px',
                marginRight: '10px'
              }" @click="bigImage(scope.row.image)" />
              <div>
                <div>{{ scope.row.name }}</div>
                <div>{{ scope.row.id }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="level_id" label="等级" align="center">
          <template slot-scope="scope">
            <span :style="{
              backgroundColor: scope.row.level_color,
              color: '#fff',
              padding: '10px',
              height: '20px',
              lineHeight: '20px',
              textAlign: 'center',
              width: '100px',
              borderRadius: '10px'
            }">
              {{ scope.row.lt_level.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" align="center"></el-table-column>

        <el-table-column prop="created_at" label="时间" align="center"></el-table-column>
      </el-table>

      <el-pagination background style="text-align:center;margin:20px 0;" :current-page="selectPrizeParams.page"
        :page-sizes="pageSizes" layout="total,prev, pager, next,sizes" :total="selectPrizeData.total"
        @size-change="changeSelectPrizeSize" @current-change="changeSelectPrizePage"></el-pagination>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="selectPrizeSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑赏品" :visible.sync="editPrizeStatus">
      <el-form :model="editPrizeParams">
        <el-form-item label="赏品信息" label-width="100px">
          <div style="display: flex;align-items: center;">
            <img :src="editPrizeParams.goods.image" :style="{
              width: '50px',
              height: '50px',
              borderRadius: '5px',
              marginRight: '10px'
            }" @click="bigImage(editPrizeParams.goods.image)" />
            <div>
              <div>{{ editPrizeParams.goods.name }}</div>
              <div>赏品ID：{{ editPrizeParams.id }}</div>
            </div>
          </div>
        </el-form-item>

        <el-form-item label="等级" label-width="100px">
          <el-select v-model="editPrizeParams.level_id" disabled style="width:200px">
            <el-option v-for="item in levelData" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="价格" label-width="100px">
          <el-input v-model="editPrizeParams.price" autocomplete="off" style="width:200px" disabled></el-input>
        </el-form-item>
        <el-form-item label="兑换价" label-width="100px">
          <el-input v-model="editPrizeParams.recovery_price" autocomplete="off" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="概率" label-width="100px">
          <el-input v-model="editPrizeParams.probability" autocomplete="off" style="width:200px">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editPrizeSubmit">保存</el-button>
      </div>
    </el-dialog>

    <SimpleImageUpload :cardStatus="cardStatus" :imageFormatArr="imageFormatArr" :imageData="imageData"
      :imageLoading="imageLoading" :uploadImageAction="uploadImageUrl" :multiple="multiple"
      @uploadImageSuccess="uploadFileSuccess" @uploadImageBefore="uploadImageBefore" @changeImageSize="changeImageSize"
      @changeImagePage="changeImagePage" @changeImageFormat="changeImageFormat" @choiceImageConfirm="choiceImageConfirm"
      @closeImage="closeImage" @getImages="getImages"></SimpleImageUpload>

    <el-dialog :visible.sync="showRenwal" style="width: 1200px; margin: 0 auto">
      <template v-slot:default>
        <div>
          <el-form>
            <el-form-item label="续箱数量">
              <el-input size="small" v-model="renwal_num" style="width: 300px;"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template v-slot:footer>
        <el-button type="primary" @click="changeRenewal">确定</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import api from "../../api/api.js";
import SimpleImageUpload from "@/components/SimpleImageUpload/index.vue";

export default {
  name: "user",
  components: {
    SimpleImageUpload
  },
  data() {
    return {
      status: "index", // 页面控制
      data: { data: [] }, // 表格诗句
      levelData: [],
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      },
      searchParams: {
        // 搜索参数
        page: 1,
        limit: 10,
        name: "",
        catId: "",
      },
      editParams: {
        name: "",
        cat_id: "",
        image: "",
        sort: "",
        price: "",
        custom_user_id:"",
        custom_days:"",
      },
      statusDatas: [
        { id: 0, name: "下架" },
        { id: 1, name: "上架" }
      ],
      catDatas: [
        { id: 1, name: "热门盲盒" },
        { id: 2, name: "快乐加倍" }
      ],
      floorsData: [
        { id: 1, name: "一级" },
        { id: 2, name: "二级" },
        { id: 3, name: "三级" },
      ],
      catData: [
        { id: 1, name: "擂台赛" },
        { id: 2, name: "天选赏" },
        { id: 3, name: "PK赏" },
        { id: 4, name: "多随机" },
        { id: 7, name: "全局赏" },
        { id: 6, name: "BOX赏" },
        { id: 8, name: "福利赏" },
        { id: 10, name: "梦想套" },
      ],

      rules: {
        // 验证规则
        name: [{ required: true, message: "请输入擂台名称", trigger: "blur" }],
        cat_id: [
          { required: true, message: "请输入擂台分类", trigger: "blur" }
        ],
        price: [
          { required: true, message: "请输入单抽价格", trigger: "change" }
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        income_value: [
          { required: true, message: "请输入临界值", trigger: "blur" }
        ],
        income_id: [
          { required: true, message: "请输入临界商品", trigger: "blur" }
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        probability: [
          { required: true, message: "请输入概率", trigger: "blur" }
        ],
        image: [{ required: true, message: "请上传图片", trigger: "blur" }]
      },

      activeValue: 1,
      inactiveValue: 0,

      cardStatus: false,
      imageFormatArr: [
        { id: "", title: "全部" },
        { id: 1, title: "商品图片" },
        { id: 2, title: "商品详情图" },
        { id: 3, title: "banner" },
        { id: 4, title: "活动" },
        { id: 5, title: "盲盒" }
      ],
      imageParams: {
        format: "",
        page: 1
      },
      imageData: {},
      imageLoading: false,
      uploadImageUrl: "/admin/common/upload?format=5",
      multiple: false,
      scene: 1, // 打开组件场景
      extra: {},

      prizeData: { data: [] },
      prizeSearchParams: {
        challengeBoxId: "",
        name: "",
        boxs: "",
        levelId: "",
        num: 0,
        page: 1,
        limit: 10
      },
      prizeTableLoading: true, // 赏品表格加载中
      prizeStatus: false,
      submitPrizeData: [],
      selectPrizeData: { data: [] },
      addPrizeStatus: false,
      addPrizeData: [],
      editPrizeStatus: false,
      editPrizeParams: {
        id: "",
        goods: {}
      },
      addPrizeLoading: true,
      selectPrizeParams: { levelId: "", challengeBoxId: "", page: 1, limit: 20 },
      logData: { data: [] },
      logSearchParams: {
        // 搜索参数
        userId: "",
        levelId: "",
        catId: "",
        page: 1,
        limit: 10,
        challenge_box_id: "",
        startTime: "",
        endTime: ""
      },
      logTableLoading: true,
      logTitle: "",
      giveParams: {
        userId: "",
        phone: "",
      },
      givePrizes: {
        data: []
      },
      selectUsers: [],
      currentUsers: [],
      challenge_box_id: [],
      giveShow: false,
      giveChoose: false,
      givePrizeId: 0,
      giveGoodsId: 0,
      showRenwal: false,
      renwal_id: 0,
      renwal_num: 1
    };
  },
  created() {
    this.getData();
    this.getLevel();
  },

  methods: {
    changeRenewal() {
      this.showRenwal = false
      api.challengeBox
        .renwal({
          id: this.renwal_id,
          num: this.renwal_num
        })
        .then(res => {
          if (res.data.code === 200) {
            this.$message({
              message: "续箱成功",
              type: "success"
            });
          } else {
            this.$message({
              message: "续箱失败",
              type: "error"
            });
          }
        });
    },
    showRenwalDialog(id) {
      this.showRenwal = true;
      this.renwal_id = id;
    },
    searchUser() {
      this.giveParams.page = 1
      this.queryUser()
    },
    chooseDone() {
      this.giveChoose = false
      this.selectUsers.push(...this.currentUsers)
      this.currentUsers = []
    },
    addGiveUser() {
      this.giveChoose = true
      this.givePrizes = []
      this.queryUser()
    },
    // giveSubmit() {
    //   const ids = this.selectUsers.map(v => v.id)
    //   console.log(ids, this.givePrizeId)
    //   api.users
    //     .giveUserPrize({ids, blind_box_id: this.givePrizeId,prize_id:this.giveGoodsId})
    //     .then(res => {
    //       if (res.data.code == 200) {
    //         this.$message({
    //           message: '赠送成功',
    //           type: 'success'
    //         });
    //         this.giveShow = false;
    //       }
    //     })
    //     .catch(error => {
    //       console.log(error);
    //     });
    //   this.selectUsers = []
    //   this.currentUsers = []
    // },
    queryUser(id) {
      api.users
        .getUserInfo(id, { params: this.giveParams })
        .then(res => {
          if (res.data.code == 200) {
            this.givePrizes = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // giveUser(e) {
    //   console.log(e)
    //   console.log(e.blind_box.id)
    //   this.giveGoodsId = e.id
    //   this.givePrizeId = e.blind_box.id
    //   this.giveShow = true
    //   this.selectUsers = []
    //   this.currentUsers = []
    //   this.queryUser(e.blind_box.id)
    //   // this.blind_box_id = e.blind_box.id
    // },
    handleSelectionChange(val) {
      this.currentUsers = val
    },
    log(row) {
      this.logSearchParams.blind_box_id = row.id;
      this.logTitle = row.name;
      this.status = "log";
      this.getLog();
    },
    getLog() {
      this.logTableLoading = true;
      api.challengeBoxOrderPrizes
        .record({ params: this.logSearchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.logTableLoading = false;
            this.logData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    editPrizeSubmit() {
      this.load();
      api.challengeBoxPrizes
        .edit(this.editPrizeParams.id, this.editPrizeParams)
        .then(res => {
          this.unload();
          if (res.data.code == 200) {
            this.$message.success(res.data.msg);
            this.editPrizeStatus = false;
            this.getPrize();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    editPrize(id) {
      this.load();
      api.challengeBoxPrizes
        .show(id)
        .then(res => {
          this.unload();
          if (res.data.code == 200) {
            this.editPrizeParams = res.data.data;
            this.editPrizeStatus = true;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 最终确定选择赏品
    addPrizeSubmit() {
      if (this.submitPrizeData.length == 0) {
        this.$message.error("请选择赏品");
        return;
      }

      for (var i = 0; i < this.submitPrizeData.length; i++) {
        if (!this.submitPrizeData[i].recovery_price) {
          this.$message.error("请输入兑换价");
          return false;
        }
      }
      api.challengeBoxPrizes
        .store({
          challengeBoxId: this.prizeSearchParams.challengeBoxId,
          prize: this.submitPrizeData
        })
        .then(res => {
          if (res.data.code == 200) {
            this.prizeStatus = false;
            this.submitPrizeData = [];
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    selectPrizeSubmit() {
      this.submitPrizeData = this.addPrizeData;
      this.addPrizeStatus = false;
      this.addPrizeData = [];
    },
    changePrize(e) {
      this.addPrizeData = e;
    },
    getSelectPrize() {
      this.addPrizeData = [];
      this.addPrizeLoading = true;
      this.selectPrizeParams.challengeBoxId = this.prizeSearchParams.challengeBoxId;
      api.common
        .getChallengePrize({ params: this.selectPrizeParams })
        .then(res => {
          if (res.data.code == 200) {
            this.addPrizeLoading = false;
            this.selectPrizeData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    selectPrize() {
      this.addPrizeStatus = true;
      this.getSelectPrize();
    },
    // 显示第一个选择赏品框
    addPrize() {
      this.prizeStatus = true;
    },
    prize(id) {
      this.status = "prize";
      this.prizeSearchParams.challengeBoxId = id;
      this.getPrize();
    },
    getPrize() {
      this.prizeTableLoading = true;
      api.challengeBoxPrizes
        .index({ params: this.prizeSearchParams })
        .then(res => {
          this.prizeTableLoading = false;
          if (res.data.code == 200) {
            this.prizeData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    changeStatus(e) {
      this.load();
      api.challengeBox
        .status(e.id, { status: e.status })
        .then(res => {
          this.unload();
          if (res.data.code === 200) {
            this.$message.success("保存成功");
            this.init();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 组件图片上传成功取消遮罩重新请求数据
    uploadFileSuccess(res) {
      this.unload();
      this.getImages();
      this.$message.success(res.msg);
    },
    // 查看图片
    getImages() {
      this.cardStatus = true;
      this.imageLoading = true;
      api.images
        .index({ params: this.imageParams })
        .then(res => {
          this.imageLoading = false;
          if (res.data.code == 200) {
            this.imageData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 组件确定选择图片
    choiceImageConfirm(choiceImages) {
      if (this.scene == 1) {
        this.editParams.goods_images = this.editParams.goods_images.concat(
          choiceImages
        );
      } else if (this.scene == 2) {
        this.editParams.image = choiceImages[0];
      } else if (this.scene == 3) {
        this.extra.row.image = choiceImages[0];
      } else if (this.scene == 4) {
        var html = "";
        choiceImages.map(item => {
          html += `<div><img src="${item}" /></div>`;
        });
        this.setHtml(html);
        console.log("html:", html);
      }
    },
    // 改变图片组件每页条数时
    changeImageSize(limit) {
      this.imageParams.limit = limit;
      this.getImages();
    },
    // 改变图片组件当前页码时
    changeImagePage(page) {
      this.imageParams.page = page;
      this.getImages();
    },
    // 改变图片组件每页条数时
    changePrizeSize(limit) {
      this.prizeSearchParams.limit = limit;
      this.getPrize();
    },

    // 改变图片组件当前页码时
    changePrizePage(page) {
      this.prizeSearchParams.page = page;
      this.getPrize();
    },
    // changeSelectPrizeSize

    changeSelectPrizeSize(limit) {
      this.selectPrizeParams.limit = limit;
      this.getSelectPrize();
    },
    changeSelectPrizePage(page) {
      this.selectPrizeParams.page = page;
      this.getSelectPrize();
    },

    // 改变图片组件每页条数时
    changeLogSize(limit) {
      this.logSearchParams.limit = limit;
      this.getLog();
    },
    // 改变图片组件当前页码时
    changeLogPage(page) {
      this.logSearchParams.page = page;
      this.getLog();
    },
    // 打开组件图片
    openImage(scene, multiple, extra) {
      this.multiple = multiple; // 单选还是多选
      this.scene = scene;
      this.extra = extra; // 额外数据
      this.cardStatus = true;
      this.getImages();
    },
    uploadImageBefore() {
      this.load();
    },
    closeImage() {
      this.cardStatus = false;
    },
    // 重置图片搜索参数
    initImage(type) {
      this.imageParams.format = type;
      this.imageParams.page = 1;
    },
    // 选择类型筛选图片
    changeImageFormat(type) {
      this.initImage(type);
      this.getImages();
    },
    // 查看大图
    bigImage(url) {
      this.$alert("图片", {
        message: "<img  src=" + url + " style='height:200px;width:200px'>",
        dangerouslyUseHTMLString: true
      });
    },
    back() {
      this.status = "index";
      this.logSearchParams.page = 1;
      this.prizeSearchParams.page = 1;
    },
    // 获取所有等级
    getLevel() {
      api.common
        .goodsChallengeLevels()
        .then(res => {
          if (res.data.code === 200) {
            this.levelData = res.data.data;
          }
        })
        .catch(error => {
        });
    },

    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },

    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.challengeBox
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code === 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.searchParams.name = "";
      this.searchParams.startTime = "";
      this.searchParams.endTime = "";
      this.searchParams.catId = "";
      this.init();
    },
    submit(formName, type) {
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          // 添加
          this.load();
          if (type === 1) {
            api.challengeBox
              .store(this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code === 200) {
                  this.init();
                }
              })
              .catch(error => {
                console.log(error);
              });
          } else if (type === 2) {
            // 编辑
            api.challengeBox
              .edit(this.editParams.id, this.editParams)
              .then(res => {
                this.unload();
                if (res.data.code === 200) {
                  this.init();
                }
              })
              .catch(error => {
                console.log(error);
              });
          }
        } else {
          this.$message.error(Object.values(error)[0][0].message);
          return false;
        }
      });
    },
    del(id, index) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          this.load();
          api.challengeBoxPrizes
            .del(id)
            .then(res => {
              this.unload();
              if (res.data.code === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.data.data.splice(index, 1);
                this.data.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    delPrize(id, index) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          this.load();
          api.challengeBoxPrizes
            .del(id)
            .then(res => {
              this.unload();
              if (res.data.code === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.prizeData.data.splice(index, 1);
                this.prizeData.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 初始化
    init() {
      this.editParams = {
        name: "",
        image: "",
        sort: "",
        price: "",
        cat_id: "",
      };
      this.back();
      this.getData();
    },
    //改变兑换价格
    updateRecoveryPrice(row) {
      this.$nextTick(() => {
        this.load();
        api.challengeBoxPrizes
          .updateRecoveryPrice(row)
          .then(res => {
            if (res.data.code === 200) {
              this.unload();
              this.$message({
                type: "success",
                message: "修改成功!"
              });
              this.status = "edit";
              this.editParams = res.data.data;
            }
          })
          .catch(error => {
            console.log(error);
          });
      });
    },
    //改变参考价
    updateShowPrice(row) {
      this.$nextTick(() => {
        this.load();
        api.challengeBoxPrizes
          .updateShowPrice(row)
          .then(res => {
            if (res.data.code === 200) {
              this.unload();
              this.$message({
                type: "success",
                message: "修改成功!"
              });
              this.status = "edit";
              this.editParams = res.data.data;
            }
          })
          .catch(error => {
            console.log(error);
          });
      });
    },
    //改变概率
    updateProbability(row) {
      this.$nextTick(() => {
        this.load();
        api.challengeBoxPrizes
          .updateProbability(row)
          .then(res => {
            if (res.data.code === 200) {
              this.unload();
              this.$message({
                type: "success",
                message: "修改成功!"
              });
              this.status = "edit";
              this.editParams = res.data.data;
            }
          })
          .catch(error => {
            console.log(error);
          });
      });
    },
    // 改变每页条数时
    usersChangeSize(limit) {
      this.giveParams.limit = limit;
      this.queryUser();
    },
    // 改变当前页码时
    usersChangePage(page) {
      this.giveParams.page = page;
      this.queryUser();
      this.selectUsers.push(...this.currentUsers)
      this.currentUsers = []
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    },
    add() {
      this.status = "add";
    },
    // 添加页面
    detail(id) {
      this.load();
      api.challengeBox
        .show(id)
        .then(res => {
          if (res.data.code === 200) {
            this.unload();
            console.log(res, '要传递的参数')
            this.status = "edit";
            this.editParams = res.data.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped lang="less">
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
